unit FAsociacionPapel_Proveed;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, XDBGrids, Buttons, DBCtrls, ExtCtrls, StdCtrls, DB,
  ZAbstractRODataset, ZDataset, CheckLst, StrUtils, ZAbstractDataset;

type
  TFormAsociacionPapel_Proveed = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    BtnAnadir: TSpeedButton;
    XDBGrid1: TXDBGrid;
    pBotones: TPanel;
    qTipos_Papel_Proveed: TZReadOnlyQuery;
    dsTipos_Papel_Proveed: TDataSource;
    cbPapel: TListBox;
    cbProveed: TListBox;
    BtnEliminar: TSpeedButton;
    BtnSalir: TSpeedButton;
    procedure FormShow(Sender: TObject);
    procedure BtnAnadirClick(Sender: TObject);
    procedure BtnSalirClick(Sender: TObject);
    procedure BtnEliminarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormAsociacionPapel_Proveed: TFormAsociacionPapel_Proveed;

implementation

uses
  ModDatos;

{$R *.dfm}

procedure TFormAsociacionPapel_Proveed.FormShow(Sender: TObject);
var
  StrSQL: String;
begin
  StrSQL := 'SELECT CONCAT( IDTIPODEPAPEL, '' - '',DESCRIPCION) ' +
            'FROM ALM_TIPOSDEPAPEL';
  md.AbreQuery( md.Query, StrSQL);
  While not md.Query.Eof do
  begin
    cbPapel.Items.Add( md.Query.Fields[ 0].AsString);
    md.Query.Next;
  end;

  StrSQL := 'SELECT CONCAT( IDPROVEEDOR, '' - '', RAZONSOCIAL) ' +
            'FROM ALM_PROVEEDORES';
  md.AbreQuery( md.Query, StrSQL);
  While not md.Query.Eof do
  begin
    cbProveed.Items.Add( md.Query.Fields[ 0].AsString);
    md.Query.Next;
  end;
  md.Query.Close;

  qTipos_Papel_Proveed.Open;
end;

procedure TFormAsociacionPapel_Proveed.BtnAnadirClick(Sender: TObject);
var
  StrSQL: String;
begin
  if ( cbPapel.ItemIndex > -1) and ( cbProveed.ItemIndex > -1) then
  begin
    StrSQL := 'INSERT INTO TIPOS_PAPEL_PROVEEDOR( IDTIPODEPAPEL, IDPROVEEDOR) ' +
              'VALUES( ' + QuotedStr( LeftStr( cbPapel.Items[ cbPapel.ItemIndex], 3)) + ', ' + QuotedStr( LeftStr( cbProveed.Items[ cbProveed.ItemIndex], 3)) + ')';
    md.Ejecuta( StrSQL);
    qTipos_Papel_Proveed.Close;
    qTipos_Papel_Proveed.Open;
  end;
end;

procedure TFormAsociacionPapel_Proveed.BtnSalirClick(Sender: TObject);
begin
  qTipos_Papel_Proveed.Close;
  Close;
end;

procedure TFormAsociacionPapel_Proveed.BtnEliminarClick(Sender: TObject);
var
  StrSQL: String;
  Num   : Integer;
begin
  StrSQL := 'DELETE FROM TIPOS_PAPEL_PROVEEDOR ' +
            'WHERE IDTIPODEPAPEL = ' + QuotedStr( qTipos_Papel_Proveed.FieldByName( 'IDTIPODEPAPEL').AsString) + ' AND IDPROVEEDOR = ' + QuotedStr( qTipos_Papel_Proveed.FieldByName( 'IDPROVEEDOR').AsString);
  md.Ejecuta( StrSQL);
  Num := qTipos_Papel_Proveed.RecNo;
  qTipos_Papel_Proveed.Close;
  qTipos_Papel_Proveed.Open;
  if qTipos_Papel_Proveed.RecordCount >= Num then
    qTipos_Papel_Proveed.RecNo := Num;

end;

end.
